package com.fieldchain.ex.fa100.ui.c2c.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.C2CBuyBean;
import com.fieldchain.ex.fa100.custom.dialog.PromptDialog;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/18
 */
public class C2CBuyDetailsActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TextView tvName;
    private TextView tvBankname;
    private TextView tvChannel;
    private TextView tvBankid;
    private TextView tvCopy1;
    private TextView tvNum;
    private TextView tvBeizhu;
    private TextView tvCopy2;
    private TextView tvStatus;
    private TextView tvSure;
    private TextView tvDelete;

    private String[] str = {"未付款", "已付款", "已取消", "已审核", "已驳回", "已过期"};
    private C2CBuyBean c2CBuyBean;
    private int id;
    private int status;
    private PromptDialog dialog;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        tvName = findViewById(R.id.tv_name);
        tvBankname = findViewById(R.id.tv_bankname);
        tvChannel = findViewById(R.id.tv_channel);
        tvBankid = findViewById(R.id.tv_bankid);
        tvCopy1 = findViewById(R.id.tv_copy1);
        tvNum = findViewById(R.id.tv_num);
        tvBeizhu = findViewById(R.id.tv_beizhu);
        tvCopy2 = findViewById(R.id.tv_copy2);
        tvStatus = findViewById(R.id.tv_status);
        tvSure = findViewById(R.id.tv_sure);
        tvDelete = findViewById(R.id.tv_delete);
    }

    @Override
    public void setValue() {
        id = getIntent().getIntExtra("id",0);

        tvCenter.setText("订单详情");
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        tvCopy1.setOnClickListener(this);
        tvCopy2.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_c2cbuydetails;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.tv_sure:
                sure();
                break;
            case R.id.tv_copy1:
                copy(tvBankid);
                break;
            case R.id.tv_copy2:
                copy(tvBeizhu);
                break;
            case R.id.tv_delete:
                if(status==1){
                     dialog=new PromptDialog(mContext);
                    dialog.show();
                }
                delete();
                break;
        }
    }

    public void copy(TextView tv) {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        ClipData clipData = ClipData.newPlainText("", tv.getText().toString());
        cm.setPrimaryClip(clipData);
        MyToast.show(this, "复制成功");
    }

    public void getData(){
        String url= Constant.base+"exchanges/"+id;
        HttpRequest.getInstance().getRequest(url, new BeanRequest<C2CBuyBean>() {
            @Override
            public void onSucess(C2CBuyBean c2CBuyBean, int code, String msg) {
                if (c2CBuyBean != null) {
                    tvName.setText(c2CBuyBean.getMerchantCard().getHolder());
                    tvBankname.setText(c2CBuyBean.getMerchantCard().getBank());
                    tvChannel.setText(c2CBuyBean.getMerchantCard().getBranch());
                    tvBankid.setText(c2CBuyBean.getMerchantCard().getBankCard());
                    tvNum.setText(c2CBuyBean.getInfo().getAmount() + "");
                    tvBeizhu.setText(c2CBuyBean.getInfo().getUniqueNo() + "");
                    status=c2CBuyBean.getInfo().getStatus();
                    tvStatus.setText(str[status]);
                    if(status>0){
                        tvSure.setVisibility(View.GONE);
                        tvDelete.setVisibility(View.GONE);
                    }else {
                        tvSure.setVisibility(View.VISIBLE);
                        tvDelete.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void sure(){
        String url= Constant.base+"exchanges/recharge/"+id;
        HashMap map=new HashMap();
        map.put("orderId",id+"");
        HttpRequest.getInstance().postRequest(map,url, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                MyToast.show(msg);
                if(code==200) {
                    mContext.startActivity(new Intent(mContext, C2CHistoryActivity.class));
                    finish();
                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void delete() {
        HashMap map = new HashMap();
        map.put("orderId", id + "");
        String url = Constant.c2cDelete + id;
        HttpRequest.getInstance().postRequest(map, url, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                MyToast.show(msg+"");
                if(code==200)finish();
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }
}
